<template>
	<view>
		<view class="ImgBox flex" style="justify-content: start;">
			<image  v-for="(item,index) in imageList" :src="item.url" mode=""></image>
			<image  @click="upload()" src="../static/images/addImg.png" mode="" v-if="imageList.length<2"></image>
		</view>
		<view class="Main">
			<view class="Title">
				<input maxlength="20" type="text" v-model="title" placeholder="填写标题会有更多赞哦~" />
			</view>
			<view class="content">
				<textarea v-model="content" placeholder="添加正文" />
			</view>
			<view class="label flex" @click="addGoods()">
				<view class="flex">
					<image class="Ico" src="../static/images/Goods.png" mode=""></image>
					关联商品
				</view>
				<image class="rightArrow" :src="`${picUrl}/w/rt.png`" mode=""></image>
			</view>
			<view class="" v-if="goodsInfo.length">
				<view class="flex" v-for="(item,index) in goodsInfo" :key="index" style="justify-content: start;margin-top: 20rpx;">
					<view class="" style="width: 100rpx;height: 100rpx;overflow: hidden;border-radius: 20rpx;margin-right: 20rpx;">
						<image style="width: 100rpx;border-radius: 20rpx;" :src="item.goods_image_url" mode="widthFix"></image>
					</view>
					<view class="" style="line-height: 30rpx;">
						<view class="" style="font-size: 30rpx;color: #333;font-weight: bold;width: 500rpx;text-overflow: ellipsis;overflow: hidden;white-space: nowrap;">
							{{item.goods_name}}
						</view>
						<view class="">
							￥{{item.goods_price}}
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="Btn" @click="Submit()">发布</view>
	</view>
</template>

<script>
	const requImg = require("@/common/api_sign/requImg.js")
	export default {
		data() {
			return {
				imageList:[],
				imageList2:[],
				title:"",
				content:"",
				goodsInfo:""
			}
		},
		onShow() {
			if(uni.getStorageSync("goodsInfo")){
				this.goodsInfo = uni.getStorageSync("goodsInfo")
			}
		},
		onLoad() {
			 
			uni.setNavigationBarTitle({
			    title:"发布"
			});
		},
		methods: {
			Submit(){//发布动态
				let that=this
				this.$util.request({
					url: '/hao/index.php?act=api-note_publish&op=save',
					method: 'post',
					data: {
						title:this.title,
						content:this.content,
						goods:uni.getStorageSync("goodsIDs"),
						images:JSON.stringify(that.imageList),
						'topic_id':"",
						'draft':0,
						'note_id':""
					},
				}).then((res)=>{
					if (res.error_code == 0) {
						uni.showToast({
							title: '发布成功',
							icon: "none",
							success:result=>{
								setTimeout(()=>{
									uni.navigateBack({
										
									})
								},1500)
							}
						});
						// this.videoList=res.datas.list
						
					} else {
						uni.showToast({
							title: res.message,
							icon: "none"
						});
					}
				})
			},
			upload(){//上传图片
				let that = this;
				that.Imgdata="";
				if(that.imageList.length<3){
						uni.chooseImage({
						 count:1,
						 sizeType: ['compressed'], //可以指定是原图还是压缩图，默认二者都有
						 sourceType: ['album','camera'], //从相册选择
						 success: function (res) {
								const tempFilePaths = res.tempFilePaths;
					
								let  data = requImg.request("index.php?act=upload&op=uploadFile&buss_type="+that.$uploadType.uploadtype012);
								 var uper = uni.uploadFile({
								  url:that.$apiurl+"index.php?act=upload&op=uploadFile",
								  filePath:res.tempFilePaths[0],
								  name: 'file',
								  formData: {
								    key:data.key,
									buss_type:data.buss_type,
									api_sign:data.api_sign,
									comefrom:data.comefrom,
									api_time:data.api_time,
									// api_member_id:data.api_member_id,
									// api_member_name:data.api_member_name,
									// name:"name"
								 },
								  success(res1) {
									  if(res1.statusCode==200){
										  console.log(JSON.parse(res1.data),'----------');
										  let ImgInfo=JSON.parse(res1.data)
										  if(ImgInfo.error_code==0){
											   // 'type' => 'image', 'file' => $upload->file_name, 'url' => $file_url
										  	let arr_tem={
												type:'image',
												file:ImgInfo.datas.file_name,
												url:ImgInfo.datas.file_url
											}
											
											that.imageList.push(arr_tem)
										  	
										  }else{
										  	uni.showToast({
											  icon:"none",
											  title:ImgInfo.message
										  	})
										  }
										  // that.Imgdata=ImgInfo.datas;
									  }else{
										  uni.showToast({
											  icon:"none",
											  title:"图片上传失败"
										  })
									  }
								  },
								})
						 },
						 error : function(e){
							console.log(e);
						 }
						});
				}else{
					uni.showToast({
						icon:"none",
						title:"最多上传三张图片"
					})
				}
				 
			},
			
			addlabel(){
				uni.navigateTo({url:"addlabel"})
			},
			addGoods(){
				uni.navigateTo({url:"relation"})
			}
		}
	}
</script>

<style>
	
	.Btn{
		background-color: #FF2549;
		color: #FFFFFF;
		text-align: center;
		margin: 100rpx 20rpx 0;
		border-radius: 30rpx;
		line-height: 60rpx;
	}
	.label{
		padding: 20rpx 0;
		border-bottom: 1rpx solid #EEEEEE;
	}
	.label .Ico{
		width: 40rpx;
		height: 40rpx;
		margin-right: 20rpx;
	}
	.rightArrow{
		width: 20rpx;
		height: 30rpx;
	}
	.content{
		border-bottom: 1rpx solid #EEEEEE;
		padding: 10rpx 0;
	}
	.Title{
		border-bottom: 1rpx solid #EEEEEE;
		padding: 10rpx 0;
		font-size: 26rpx;
	}
	.Main{
		margin: 20rpx;
	}
	.ImgBox{
		padding: 20rpx;
	}
	.ImgBox image{
		width: 220rpx;
		height: 220rpx;
		margin-right:20rpx ;
	}
	page{
		background-color: #FFFFFF;
	}
	.flex{
		display: flex;
		justify-content: space-between;
		align-items: center;
	}
</style>
